package com.zenika.zenbinding.client.converter;

import com.zenika.zenbinding.client.exception.ConverterException;

/**
 * Converter for a String[] Object to Boolean.
 * 
 * This converter is instantiating with an Array of 2 Strings, ONLY 2 Strings
 * 
 * This means if method fromObjectToString receives a Boolean, values[0] will be returned if Boolean is true, values[1]
 * will be returned if Boolean is false
 * 
 * if method fromStringToObject receives a String, true will be returned if String matches values[0], false will be
 * returned if String matches values[1]
 * 
 * @author Zenika
 */
public class Radio2BooleanConverter implements Converter {

	/**
	 * values MUST BE an array of ONLY 2 Strings
	 */
	private String[] values;

	/**
	 * @param valeurs
	 * 
	 * if String to convert matches values[0] : true, if String to convert matches values[1] : false
	 * 
	 */
	public Radio2BooleanConverter(String[] valeurs) {
		this.values = valeurs;
	}

	public String fromObjectToString(Object object) throws ConverterException {
		Boolean bool = (Boolean) object;
		return this.values[bool.booleanValue() ? 0 : 1];
	}

	public Object fromStringToObject(String string) throws ConverterException {
		return new Boolean(this.values[0].equals(string));
	};

}
